// XPEFastBootDlg.cpp : implementation file
//
// Copyright (c) 2004 SJJ Embedded Micro Solutions, LLC
// All Rights Reserved
// Code is provided AS IS without any warranty

#include "stdafx.h"
#include "XPEFastBoot.h"
#include "XPEFastBootDlg.h"
#include ".\xpefastbootdlg.h"
#include "ewfapi.h"

extern "C"
{
#include "xpepm.h"
}

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CXPEFastBootDlg dialog



CXPEFastBootDlg::CXPEFastBootDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CXPEFastBootDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CXPEFastBootDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CXPEFastBootDlg, CDialog)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON5, OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnBnClickedButton6)
	ON_BN_CLICKED(IDC_EWFEnable, OnBnClickedEwfenable)
	ON_BN_CLICKED(IDC_EWFDisable, OnBnClickedEwfdisable)
	ON_BN_CLICKED(IDC_COMMIT, OnBnClickedCommit)
	ON_BN_CLICKED(IDC_CANDD, OnBnClickedCandd)
END_MESSAGE_MAP()


// CXPEFastBootDlg message handlers

BOOL CXPEFastBootDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	
	UpdateEWFStatus();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CXPEFastBootDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CXPEFastBootDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CXPEFastBootDlg::OnBnClickedOk()
{
	
	OnOK();
}

void CXPEFastBootDlg::OnBnClickedButton3()
{
	
XPE_Shutdown();

}

void CXPEFastBootDlg::OnBnClickedButton5()
{
	
XPE_Hibernate(TRUE);

}

void CXPEFastBootDlg::OnBnClickedButton6()
{
	
XPE_ReStart( );
}

void CXPEFastBootDlg::OnBnClickedEwfenable()
{
	
HANDLE hDisk;
	WCHAR szDrive[20];
	BOOL bResult =FALSE;

	swprintf(szDrive, L"\\\\.\\c:");
		
	//Next, Open the handle and check for errors
	hDisk = EwfMgrOpenProtected(szDrive);

	bResult = EwfMgrEnable(hDisk);
	if (!bResult)
	{
		MessageBox("EwfMgrEnable failed", "Error", MB_OK);
	}

	
	EwfMgrClose(hDisk);
	XPE_ReStart( );

}

void CXPEFastBootDlg::UpdateEWFStatus(void)
{

	DWORD dwStatus = ERROR_SUCCESS;
	BOOL bResult =FALSE;
	HANDLE hDisk = INVALID_HANDLE_VALUE;;
	PEWF_VOLUME_CONFIG pVolConfig = NULL;
	PEWF_VOLUME_NAME_ENTRY pProVolList = NULL;
	WCHAR szVolumeName[EWF_MAX_DEVICE_NAME_LENGTH + 15] = {0};
	WCHAR szDrive[20];
	swprintf(szDrive, L"\\\\.\\c:");
		
	//Next, Open the handle and check for errors
	hDisk = EwfMgrOpenProtected(szDrive);

	pVolConfig = EwfMgrGetProtectedVolumeConfig(hDisk);

	//Display the results in the text box

	switch (pVolConfig->Type)
		{
			case EWF_DISK:      SetDlgItemText(IDC_OTEDIT, "DISK"); break;
			case EWF_RAM:       SetDlgItemText(IDC_OTEDIT, "RAM");     break;
			case EWF_RAM_REG:   SetDlgItemText(IDC_OTEDIT, "RAM_REG"); break;
			default:
			MessageBox("ERROR: Unknown Ewf type", "Error", MB_OK);
			break;
		}

	switch (pVolConfig->State)
		{
			case EWF_ENABLED:   SetDlgItemText(IDC_STEDIT, "ON");  break;
			case EWF_DISABLED:  SetDlgItemText(IDC_STEDIT, "OFF"); break;
			default:
			MessageBox("ERROR: Unknown Ewf state","Error", MB_OK);
			break;
		}
	
	
	// Close Handle and Clear pVolConfig
	LocalFree(pVolConfig);
	pVolConfig = NULL;
	EwfMgrClose(hDisk);


}
void CXPEFastBootDlg::OnBnClickedEwfdisable()
{
	HANDLE hDisk;
	WCHAR szDrive[20];
	BOOL bResult =FALSE;

	swprintf(szDrive, L"\\\\.\\c:");
		
	//Next, Open the handle and check for errors
	hDisk = EwfMgrOpenProtected(szDrive);		
		
	bResult = EwfMgrDisable(hDisk,FALSE);
	if (!bResult)
	{
		MessageBox("EwfMgrDisable failed", "Error", MB_OK);
	}
    	
	EwfMgrClose(hDisk);
	XPE_ReStart( );

}


void CXPEFastBootDlg::OnBnClickedCommit()
{
	HANDLE hDisk;
	WCHAR szDrive[20];
	BOOL bResult =FALSE;

	swprintf(szDrive, L"\\\\.\\c:");
		
	//Next, Open the handle and check for errors
	hDisk = EwfMgrOpenProtected(szDrive);					
		
	bResult = EwfMgrCommit(hDisk);
	if (!bResult)
	{
		MessageBox("EwfMgrCommit failed", "Error", MB_OK);
	}
    	
	EwfMgrClose(hDisk);
	XPE_ReStart( );

}


void CXPEFastBootDlg::OnBnClickedCandd()
{
	HANDLE hDisk;
	WCHAR szDrive[20];
	BOOL bResult =FALSE;

	swprintf(szDrive, L"\\\\.\\c:");
		
	//Next, Open the handle and check for errors
	hDisk = EwfMgrOpenProtected(szDrive);		
		
	bResult = EwfMgrDisable(hDisk,TRUE);
	if (!bResult)
	{
		MessageBox("EwfMgrDisable failed", "Error", MB_OK);
	}
    	
	EwfMgrClose(hDisk);
	XPE_ReStart( );

}
